package com.cxrj.zhsq.administrate.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cxrj.zhsq.administrate.entity.Personnel;
import com.cxrj.zhsq.administrate.entity.Property;
import com.cxrj.zhsq.administrate.entity.Vehicle;
import com.cxrj.zhsq.administrate.service.AdministrateService;
import com.cxrj.zhsq.utils.ImportExcelUntil;

@Controller
public class AdministrateController {

	@Resource
	private AdministrateService administrateService;

	/**
	 * 分页查询人员信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectPersonnels", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectPersonnels(HttpServletRequest request, HttpServletResponse response) {
		return administrateService.selectPersonnelPager(request, response);
	}

	/**
	 * 分页查询车辆信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectVehicles", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectVehicles(HttpServletRequest request, HttpServletResponse response) {
		return administrateService.selectVehiclePager(request, response);
	}

	/**
	 * 分页查询房产信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectPropertys", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectPropertys(HttpServletRequest request, HttpServletResponse response) {
		return administrateService.selectPropertyPager(request, response);
	}

	/**
	 * 根据id查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectPersonnelById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectPersonnelById(String id, HttpServletRequest request) {
		return administrateService.selectPersonnelById(id, request);
	}

	/**
	 * 根据id查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectVehicleById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectVehicleById(String id, HttpServletRequest request) {
		return administrateService.selectVehicleById(id, request);
	}

	/**
	 * 根据id查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectPropertyById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectPropertyById(String id, HttpServletRequest request) {
		return administrateService.selectPropertyById(id, request);
	}

	/**
	 * 添加人员
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertAndUpdatePersonnel", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertAndUpdatePersonnel(Personnel personnel, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = administrateService.insertAndUpdatePersonnel(personnel);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 添加车辆
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertAndUpdateVehicle", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertAndUpdateVehicle(Vehicle vehicle, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = administrateService.insertAndUpdateVehicle(vehicle);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 添加房产
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertAndUpdateProperty", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertAndUpdateProperty(Property property, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = administrateService.insertAndUpdateProperty(property);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 生成app用户
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/generateUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> generateUser(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = administrateService.insertAppUser(id);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 上传人员Excel
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/improtPersonnelExcel", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> ImprotExcel(@RequestParam(value = "uploadFile") MultipartFile file) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> list = ImportExcelUntil.importExcel(file, new Personnel());
			List<Personnel> personnels = new ArrayList<Personnel>();
			for (Map<String, Object> map : list) {
				Personnel personnel = new Personnel();
				personnel.setId(UUID.randomUUID().toString());
				personnel.setCreateTime(new Date());
				personnel.setAddr(map.get("addr").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
}
